package Service.ServiceImpl;

import Dao.DaoStuHandle;
import Model.Student;
import Service.StudentService;

public class StudentServiceImpl implements StudentService {
    @Override
    public void showStu(Student[] stus, int len) {
        DaoStuHandle daoStuHandle = new DaoStuHandle();
        daoStuHandle.showStuDAO(stus, len);
    }

    @Override
    public void addStu(Student[] stus, Student stu) {
        DaoStuHandle daoStuHandle = new DaoStuHandle();
        daoStuHandle.addStuDAO(stus, stu);
    }

    @Override
    public boolean delStu(Student[] stus, Integer len, Integer id) {
     //判断学号是否存在
        int a = 0;
        boolean flag = false;
        for (int i = 0; i < len; i++) {
            if (stus[i].getId() == id) {
                a = i;
                flag = true;
                break;
            }
        }
        //如果存在，调用数据访问层删除数据
        if (flag) {
            DaoStuHandle daoStuHandle = new DaoStuHandle();
            daoStuHandle.delStuDAO(stus, len, a);
            return true;
        }
        return false;
    }

    @Override
    public boolean updateStu(Student[] stus, Integer len, Integer id) {
       int a=0;
       boolean flag=false;
        for (Integer i = 0; i < len; i++) {
            if (stus[i].getId()==id){
                a=i;
                flag =true;
                break;

            }
        }
        if (flag){
            DaoStuHandle daoStuHandle = new DaoStuHandle();
            daoStuHandle.updateStuDAO(stus,len,a);
            return true;
        }
        return false;
    }
}
